package com.foxit.setting;

import java.io.*;
import java.net.*;
import java.lang.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Date;
import java.util.SimpleTimeZone;
import java.text.SimpleDateFormat;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.couchbase.client.CouchbaseClient;

public class DumpUser extends HttpServlet {
   public void doGet(HttpServletRequest request, HttpServletResponse response)
                    throws ServletException, IOException {
      response.setContentType("text/html;charset=UTF-8");
      request.setCharacterEncoding("UTF-8");
      PrintWriter outp = response.getWriter();
      outp.println("<HTML>");
      outp.println("<BODY>");
      String couch = request.getParameter("couch");
      if(couch==null || couch.length()==0) {
         outp.println("couch=? no couchbase hostname");
         outp.println("</BODY></HTML>");
         return;
      }
      CouchbaseClient client=null;
      try {
         List<URI> hosts = Arrays.asList(new URI("http://"+couch+":8091/pools"));
         client = new CouchbaseClient(hosts, "default", "");
         MongoClient mongo = new MongoClient("localhost", 27017);
         DB db = mongo.getDB("settingdb");
         DBCollection t_control = db.getCollection("control"); 
         DBCollection t_adrule = db.getCollection("adrule"); 
         int i=0;
         DBCursor cursor;
         List list = new ArrayList();
         cursor = t_control.find();
         while (cursor.hasNext()) {
            Control s = (Control) JSON.parseObject(cursor.next().toString(),Control.class);
            String advertiser = s.getAdvertiser();
            String adlist = s.getAdlist();
            String user =s.getUser();
            String key = "UA_"+user;
            String value = advertiser+":"+adlist+":";
            if(advertiser.equals("ALL")) {
               client.set(key,"ALL:ALL:ALL").get();
               continue;
            } 
            if(advertiser != null && advertiser.length()!=0) {
               String[] adv=advertiser.split(",");
               for(i=0;i < adv.length;i++) {
                  BasicDBObject query = new BasicDBObject("advertiser",adv[i]);
                  DBCursor cursor1 = t_adrule.find(query);
                  while(cursor1.hasNext()) { 
                     Adrule r = (Adrule) JSON.parseObject(cursor1.next().toString(),Adrule.class);
                     list.add(r.getAdrule());
                  }
                  cursor1.close();
               }
            }
            if(adlist != null && adlist.length()!=0) {
               String[] rule=adlist.split(",");
               for(i=0;i<rule.length;i++) {
                  list.add(rule[i]);
               }
            }
            HashSet hs = new HashSet();
            hs.addAll(list);
            list.clear();
            list.addAll(hs);
            for(i=0;i < list.size();i++) {
               if(i != 0) value += ",";
               value += list.get(i).toString();
            } 
            client.set(key,value).get();
         }
         cursor.close();
      } catch(Exception ex) {
          outp.println("mongodb exception:"+ex.getMessage());
          outp.println("</HTML>");
          outp.println("</BODY>");
          return;
      } 
      response.sendRedirect("./SetControl");
   }
}
